package com.atguigu.crowd.handler;

import com.atguigu.crowd.api.MySQLRemoteService;
import com.atguigu.crowd.constant.CrowdConstant;
import com.atguigu.crowd.entity.vo.AddressVO;
import com.atguigu.crowd.entity.vo.MemberLoginVo;
import com.atguigu.crowd.entity.vo.OrderProjectVO;
import com.atguigu.crowd.util.ResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author zwj
 * @create 2021-08-13-20:33
 */
@Controller
public class OrderHandler {
    @Autowired
    private MySQLRemoteService mySQLRemoteService;

    private Logger logger = LoggerFactory.getLogger(OrderHandler.class);

    @RequestMapping("/save/address")
    public String saveAddress(AddressVO addressVO,HttpSession session){
        ResultEntity<String> resultEntity = mySQLRemoteService.saveAddressRemote(addressVO);
        logger.debug("地址保存结果" + resultEntity.getResult());
        OrderProjectVO orderProjectVO = (OrderProjectVO)session.getAttribute(CrowdConstant.ATTR_NAME_ORDER_PROJECT);
        Integer returnCount = orderProjectVO.getReturnCount();
        return "redirect:http://121.196.205.151/order/confirm/order/"+returnCount;
    }

    @RequestMapping("/confirm/order/{returnCount}")
    public String showConfirmOrderInfo(@PathVariable("returnCount") Integer returnCount,
                                       HttpSession session){
        //重置回报数量
        OrderProjectVO orderProjectVO = (OrderProjectVO)session.getAttribute(CrowdConstant.ATTR_NAME_ORDER_PROJECT);
        orderProjectVO.setReturnCount(returnCount);
        session.setAttribute(CrowdConstant.ATTR_NAME_ORDER_PROJECT,orderProjectVO);
        //获取当前用户
        MemberLoginVo memberLoginVo = (MemberLoginVo)session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
        Integer memberId = memberLoginVo.getId();
        //查询地址数据
         ResultEntity<List<AddressVO>> addressVoListResultEntity= mySQLRemoteService.getAddressVORemote(memberId);

         if (ResultEntity.SUCCESS.equals(addressVoListResultEntity.getResult())){
             List<AddressVO> voList = addressVoListResultEntity.getData();
             session.setAttribute(CrowdConstant.ATTR_NAME_ADDRESS,voList);
         }

        return "confirm-order";
    }

    @RequestMapping("/confirm/return/info/{projectId}/{returnId}")
    public String showReturnConfirmInfo(@PathVariable("projectId") Integer projectId,
                                        @PathVariable("returnId") Integer returnId,
                                        HttpSession session){
        ResultEntity<OrderProjectVO> resultEntity= mySQLRemoteService.getOrderProjectVORemote(projectId,returnId);
        String result = resultEntity.getResult();
        if(ResultEntity.SUCCESS.equals(result)){
            OrderProjectVO resultEntityData = resultEntity.getData();
            session.setAttribute(CrowdConstant.ATTR_NAME_ORDER_PROJECT,resultEntityData);
        }
        return "confirm-return";
    }
}
